<?php
error_reporting(E_ALL);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcessEmailStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcessEmailFinish' );

function jeProcessEmailFinish()
{
    global $database, $my;
    
    $processor = mosGetParam( $_REQUEST, 'processor'); // success or fail
    
    if($processor != '.email') return false;

    $product_id     = mosGetParam( $_REQUEST, 'product_id'); // transaction id
    $cart_order_id  = mosGetParam( $_REQUEST, 'cart_order_id'); // transaction id
    $order_number   = mosGetParam( $_REQUEST, 'order_number'); //2418397558
    $cart_id        = mosGetParam( $_REQUEST, 'cart_id'); // cart id7
    $total          = mosGetParam( $_REQUEST, 'total'); // cart id7
    $pay = mosGetParam( $_REQUEST, 'pay');
    
    $order['pay'] = mosGetParam( $_REQUEST, 'pay');
    $order['id']  = $cart_order_id;
    $order['gateway'] = $processor;
    $order['gateway_id'] = time();
    $order['price'] = mosGetParam( $_REQUEST, 'total');
    //print_r($order);
    //exit;
    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$processor}'"
    . "\n AND folder = 'system'"
    ;
    
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
  
    $email 	= $botParams->def( 'email');
    $subj 	= $botParams->def( 'subject');
    
    $headers .= "To: $email\r\n"; 
    
    $body  = "You get Payment request.\r\n";
    $body .= "-------\r\n";
    $body .= "Ures ID:    $my->id.\r\n";
    $body .= "Ures Email: $my->email.\r\n";
    $body .= "Ures Name:  $my->name.\r\n";
    $body .= "-------\r\n";
    $body .= "Product ID: $product_id.\r\n";
    $body .= "Price: $total.\r\n";
    $body .= "Subscription ID: $cart_order_id.\r\n";

    $message = html_entity_decode($body, ENT_QUOTES);
    mosMail( $email, 'Administrator', $adminEmail2, $subj, $message);
    
    return $order;
}

function jeProcessEmailStart($params)
{
    $processor = $params['processor'];
    
    if($processor != '.email') return;
   
    Global $my;
   
    $user   = $my->id;
    $email  = $my->email;
    $option = mosGetParam( $_REQUEST, 'option');
    $task   = mosGetParam( $_REQUEST, 'task');
    $task   = 'pay';
    $product_id = ($params['item_number'] ? $params['item_number'] : $params['order_id']);
    $order_id   = $params['order_id'];
    $price      = $params['order_amount'];
   
    if(!$price || !$user || !$processor || !$order_id)
    {
        echo "Error Payment Processing: some of the params passed are empty<br>$vendor $price $user $processor $order_id";
        return;
    }
    
    $param['total'] = $price;
    $param['cart_order_id'] = $order_id;
    $param['product_id'] = $product_id;
    $param['option'] = $option;
    $param['task'] = 'pay';
    $param['email'] = $email;
    $param['processor'] = $processor;
    $param['pay'] = 'fail';
     
    $url = 'index.php?'.jpArray2Url($param);
    //echo $url;
    header("location: $url");
}


if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}




?>
